$(document).ready(function() {
	var oTable;
	/**
	* Initialize the table data.
	*/
	oTable = $('#dataTables-example').dataTable(
		{
			"bProcessing" : true,
			"sAjaxSource" : "getAllCompanys.html",
			"sAjaxDataProp" : "",
			"aoColumns" : [ {"mData" : "companyId"},
				                {"mData" : "companyId"}, 
				                {"mData" : "companyName"}, 
				                {"mData" : "adminId"}, 
				                {"mData" : "companyLogoUrl"}, 
				                {"mData" : "createTime"}, 
				                {"mData" : "updateTime"} ,
				                {"mData" : "companyType"},
				                {"mData" : "companyEnabled"},
				                {"mData" : "companyId"}
				              ],
			"aoColumnDefs" : [ {
								"aTargets" : [ 0 ],
								"mData" : "选择",
								"mRender" : function(data,type, full) {
										    var contentView="";
										    contentView+="<input type='checkbox' name='companySelected_"+data+"' id='companySelected_"+data+"' value='"+data+"'/>";
										    return contentView;
										    }
									},
									
									{
										"aTargets":[3],
										"mRender" : function(data,type, full) {
											var adminUserName=null;
											$.ajax({
												type : "POST",
												url : "getUserDetail.html",
												data:{"userId":data},
												async:false,
												success : function(data) {
													
													if(data.resultCode==0){
														var user=data.data;
														adminUserName=user.userName;
														adminUserName=user.userName;
													}else{
														adminUserName="unknown";
													}
													
												}
										    });
											return adminUserName;
										}
									},
									{
										"aTargets":[5],
										"mRender" : function(data,type, full) {
											var newTime = new Date();
											newTime.setTime(data);
											return formatTime(newTime,"yyyy-MM-dd HH:mm:ss");
										}
									},
									{
										"aTargets":[6],
										"mRender" : function(data,type, full) {
											var newTime = new Date();
											newTime.setTime(data);
											return formatTime(newTime,"yyyy-MM-dd HH:mm:ss");
										}
									},
									{
										"aTargets" : [ 7 ],
										"mData" : "操作",
										"mRender" : function(data,type, full) {
														var viewContent = "";
														if(data==1){
															viewContent+="<span class='label label-success'>普通</span>";
														}else{
															viewContent+="<span class='label label-warning'>其他</span>";	
														}
														return viewContent;
													}
										},
									{
									"aTargets" : [ 8 ],
									"mData" : "操作",
									"mRender" : function(data,type, full) {
													var viewContent = "";
													if(data==1){
														viewContent+="<span class='label label-success'>启用</span>";
													}else{
														viewContent+="<span class='label label-warning'>禁用</span>";	
													}
													return viewContent;
												}
									},
									{
										"aTargets" : [ 9 ],
										"mData" : "操作",
										"mRender" : function(data,type, full) {
														var operations = "";
														operations += ' <button type="button" class="btn btn-primary btn-circle"><i class="fa fa-list"></i></button>';
													
														operations += ' <button type="button" class="btn btn-info btn-circle"><i class="fa fa-check"></i></button>';
													
														operations += ' <button type="button" class="btn btn-danger btn-circle"><i class="fa fa-heart"></i></button>';
														return operations;
													}
									} ],
				"oLanguage" : {
								 "sSearch" : "全文搜索:"
							  },
				fnInitComplete : function() {
											/**
											 * 数据下拉框初始化
											 */
									$("thead td ").each(function(i) {
														var selectInputs = $(this).children("select");
														var selectInput = null;
														if (selectInputs != null&& selectInputs.length != 0) {
															selectInput = selectInputs[0];
														}
														if (selectInput != null) {
															$('select',this).change(function() {
																			oTable.fnFilter($(this).find("option:selected").text(),i);
																		});
														}
														});
									$("#dataTables-example tbody tr").on("click", function(i) {
										       var company = oTable.fnGetData( $(this).index());
										        if ( $(this).hasClass('row_selected') ) {
										            $(this).removeClass('row_selected');
										            $("#companySelected_"+company.companyId).prop('checked',false)
										        }
										        else {
										            oTable.$('tr.row_selected').removeClass('row_selected');
										            $(this).addClass('row_selected');
										          
										            $("#companySelected_"+company.companyId).prop('checked',true)
										            
										        }
										    });
										}
									});
					// Add Check box after every row
					
					$("thead input").keyup(
							function() {
								/* Filter on the column (the index) of this element */
								oTable.fnFilter(this.value, oTable.oApi._fnVisibleToColumnIndex(oTable.fnSettings(), $("thead input").index(this)));
							});
					$("thead input").each(function(i) {
						this.initVal = this.value;
					});

					$("thead input").focus(function() {
						if (this.className == "search_init") {
							this.className = "";
							this.value = "";
						}
					});
					$("thead input").blur(function(i) {
						if (this.value == "") {
							this.className = "search_init";
							this.value = this.initVal;
						}
					});
					
				});

/**
 * 时间格式化函数，可以统一到统一函数库中
 * @param time
 * @param format
 * @returns
 */
 function formatTime(time, format){
    var t = new Date(time);
    var tf = function(i){return (i < 10 ? '0' : '') + i};
    return format.replace(/yyyy|MM|dd|HH|mm|ss/g, function(a){
        switch(a){
            case 'yyyy':
                return tf(t.getFullYear());
                break;
            case 'MM':
                return tf(t.getMonth() + 1);
                break;
            case 'mm':
                return tf(t.getMinutes());
                break;
            case 'dd':
                return tf(t.getDate());
                break;
            case 'HH':
                return tf(t.getHours());
                break;
            case 'ss':
                return tf(t.getSeconds());
                break;
        }
    })
}

